package com.xmy.cultivate.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.StudentForData;
import com.xmy.cultivate.entity.StudentForDataSet;
import com.xmy.cultivate.entity.StudentForDataType;
import com.xmy.cultivate.entity.StudentGrade;
import com.xmy.cultivate.service.IStudentForDataService;
import com.xmy.cultivate.service.IStudentForDataSetService;
import com.xmy.cultivate.service.IStudentForDataTypeService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.ConfigData;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 类型数据设置，如设置年级或是否排除补课班 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-01-17
 */
@RestController
@RequestMapping("/api/studentForDataSet")
public class StudentForDataSetController {
    @Autowired
    IStudentForDataSetService iStudentForDataSetService;

    @Autowired
    IStudentForDataTypeService iStudentForDataTypeService;

    @ApiOperation("添加年级和补课班排除情况")
    @PostMapping("/saveStudentForDataSet")
    public Result saveStudentForDataType(@RequestBody Map reMap) {
        List<Object> studentForDataSetList = (List<Object>) reMap.get("studentForDataSetList");

        for (Object object : studentForDataSetList) {
            String jsonObject = JSON.toJSONString(object);
            StudentForDataSet studentForDataSet = JSONObject.parseObject(jsonObject, StudentForDataSet.class);
            iStudentForDataSetService.save(studentForDataSet);
        }

        return new Result(Code.UPDATE_OK, "", Code.UPDATE_OK_MSG);
    }

    @ApiOperation("删除排除情况")
    @PostMapping("/removeStudentForDataSet")
    public Result removeStudentForDataSet(@RequestBody Map reMap){
        List<Object> studentForDataSetList = (List<Object>) reMap.get("studentForDataSetList");
        for (Object object : studentForDataSetList) {
            String jsonObject = JSON.toJSONString(object);
            StudentForDataSet studentForDataSet = JSONObject.parseObject(jsonObject, StudentForDataSet.class);
            iStudentForDataSetService.removeById(studentForDataSet);
        }

        return new Result(Code.OK, "", "删除成功");
    }

    @ApiOperation("修改排除情况")
    @PostMapping("/updateStudentForDataSet")
    public Result updateStudentForDataSet(@RequestBody StudentForDataSet studentForDataSet){
        iStudentForDataSetService.updateById(studentForDataSet);
        return new Result(Code.OK, "", "修改成功");
    }

    @ApiOperation("查询年级和补课班排除情况")
    @GetMapping("/getStudentForDataSet")
    public Result getStudentForDataSet(@RequestParam(value = "yearPart", required = false) String yearPart,
                                        @RequestParam(value = "quarterNum", required = false) String quarterNum,
                                        @RequestParam(value = "dataTypeId", required = false) String dataTypeId,
                                        @RequestParam(value = "type", required = false) String type){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year_part",yearPart);
        queryWrapper.eq("quarter_num",quarterNum);
        queryWrapper.eq("type",type);
        queryWrapper.eq("data_type_id",dataTypeId);
        queryWrapper.eq("status",1);
        List<StudentForDataSet> studentForDataList=iStudentForDataSetService.list(queryWrapper);
        ConfigData configData = new ConfigData();
        for(StudentForDataSet student:studentForDataList){
            if((student.getYearClassId()!=null)){
                student.setYearClassStr(configData.getYearClassForNum(student.getYearClassId()));
            }
        }

        return new Result(Code.OK, studentForDataList, "查询成功");
    }

    @ApiOperation("查询分类")
    @GetMapping("/getStudentForDataType")
    public Result getStudentForDataType(){
        List<StudentForDataType> studentForDataTypes=iStudentForDataTypeService.list();
        return new Result(Code.OK,studentForDataTypes , "查询成功");
    }

    @ApiOperation("查询年级和补课班未排除情况")
    @GetMapping("/getNotStudentForDataSet")
    public Result getNOTStudentForDataSet(@RequestParam(value = "yearPart", required = false) String yearPart,
                                       @RequestParam(value = "quarterNum", required = false) String quarterNum,
                                       @RequestParam(value = "dataTypeId", required = false) String dataTypeId,
                                       @RequestParam(value = "type", required = false) String type){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year_part",yearPart);
        queryWrapper.eq("quarter_num",quarterNum);
        queryWrapper.eq("type",type);
        queryWrapper.eq("data_type_id",dataTypeId);
        queryWrapper.eq("status",1);
        List<StudentForDataSet> studentForDataList=iStudentForDataSetService.list(queryWrapper);
        ConfigData configData = new ConfigData();
        List<String> yearClassStrList = new ArrayList<>();
        for(StudentForDataSet student:studentForDataList){
            student.setYearClassStr(configData.getYearClassForNum(student.getYearClassId()));
            yearClassStrList.add(student.getYearClassStr());
        }
        StudentForDataSetController studentForDataSet = new StudentForDataSetController();
        List<String> getMissingYearClasses=studentForDataSet.getMissingYearClasses(yearClassStrList);

        return new Result(Code.OK, getMissingYearClasses, "查询成功");
    }
    public static List<String> getMissingYearClasses(List<String> yearClassStrList) {
        List<String> allYearClasses = Arrays.asList("幼儿园","一年级","二年级","三年级","四年级","五年级","六年级","初一","初二","初三","高一","高二","高三");
        List<String> missingYearClasses = new ArrayList<>(allYearClasses);
        missingYearClasses.removeAll(yearClassStrList);
        return missingYearClasses;
    }

}
